const { Collection } = require('mongodb')
const mongoUtil = require('./mongo_util')
const collectionName = 'cart'
function addcart(data, callback) {
	mongoUtil.getCollection(collectionName).then((collection) => {
		collection.insertOne(data, function (err, res) {
			callback(err, res)
		})
	})
}
function getcart(data, callback) {
	mongoUtil.getCollection(collectionName).then((collection) => {
		collection.find(data).toArray(function (err, result) {
			callback(err, result)
		})
	})
}
function updatecart(data, callback) {
	const xx = {
		goods_id: data.goods_id,
		username: data.username,
	}
	console.log('data', data, xx)
	// var updateStr = { $set: data }
	var updateStr = { $set: data }
	mongoUtil.getCollection(collectionName).then((collection) => {
		collection.updateOne(xx, updateStr, (err, res) => {
			console.log('err===', err)
			callback(err, res)
		})
	})
}
function delcart(data, callback) {
	mongoUtil.getCollection(collectionName).then((collection) => {
		collection.deleteMany(
			{ username: data.username, goods_id: { $in: data.goods_id } },
			function (err, res) {
				callback(err, res)
			}
		)
	})
}
module.exports = {
	addcart,
	delcart,
	updatecart,
	getcart,
}
